<template>
    <div class="search">
        <el-autocomplete
            :fetch-suggestions="querySearch"
            @select="goDetail"
            :trigger-on-focus="false"
            v-model="hosname"
            clearable
            placeholder="请输入医院名称"></el-autocomplete>
        <el-button type="primary" size="default" :icon="Search">搜索</el-button>
    </div>

</template>

<script setup lang="ts">
import {
    Search,
} from '@element-plus/icons-vue'
import {ref} from "vue";
import type {HospitalInfo} from "@/api/home/type.ts";
import {reqHospitalInfo} from "@/api/home/index.ts";
import {useRouter} from "vue-router";
import "@/pages/home/search/index.vue";

//创建路由器对象
let $router = useRouter()

//收集到搜索到关键字
let hosname = ref<string>('')

//顶部搜索事件的回调
const querySearch = async (keyword:string,cb:any  ) => {
   //当用户输入完关键字，函数执行一次
    let result :HospitalInfo = await reqHospitalInfo(keyword)
    //整理数据，变成组件需要的格式
    let showData = result.data.map(item=>{
        return  {
            value:item.hosname,//医院的名字
            hoscode:item.hoscode//医院的唯一标识符
        }
    })
    //给组件提供展示的数据
    cb(showData)
}

//点击某一个推荐项
const goDetail = (item:any)=>{
    //点击推荐项跳转到详情页面,将来需要携带query参数（医院编码）
    $router.push({path: '/hospital/register',query:{hoscode:item.hoscode}})
}


</script>
<script lang="ts">
export default {
    name:"Search"
}
</script>

<style scoped lang="scss">
.search{
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin : 10px 0;
    button{
        margin-left: 6px;
    }
    /*深度选择器*/
    ::v-deep(.el-input__wrapper){
        width: 600px;
    }

}
</style>